﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameUtils : MonoBehaviour
{
    private GameObject uiRoot = null;
 
    private static GameUtils _instance;
 
    void Awake()
    {
        _instance = this;
    }

    void OnDestroy()
    {
        if(this == _instance)
        {
            _instance = null;
        }
    }
 
    public static GameUtils Instance()
    {
        return _instance;
    }
 
    public GameObject UiRoot
    {
        get
        {
            return GetUIRoot();
        }
    }
 
    private GameObject GetUIRoot()
    {
        try
        {
            if (uiRoot != null)
            {
                return uiRoot;
            }
            if (FindObjectOfType<Canvas>() != null)
            {
                uiRoot = FindObjectOfType<Canvas>().gameObject;
                return uiRoot;
            }
            else
            {
                uiRoot = GameObject.Find("Canvas").gameObject;
                return uiRoot;
            }
        }
        catch (Exception e)
        {
            Debug.Log("获取根节点失败原因：" + e);
        }
        return null;
    }
}
